﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Infrastructure.ObjectModel.Screens;
using Infrastructure.ObjectModel;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using B10_Ex02_Elad_034695114_Meyron_026607788.ObjectModel;
using B10_Ex02_EladLebovitch_034695114.ObjectModel;
using Microsoft.Xna.Framework.Input;
using Infrastructure.ServiceInterfaces;

namespace B10_Ex02_EladLebovitch_034695114.Screens
{
    public class MainMenuScreen : GameScreen
    {
        private Menu m_MainMenu;
        private InvadorBackground m_Background;

        private enum MainMenu
        {
            PlayersNum,
            ScreenOpt,
            SoundOpt,
            Play,
            Quit
        }

        private string NumOfPlayers
        {
            get
            {
                return
                    ((Game.Services.GetService(typeof(GameFlow)) as GameFlow).IsTwoPlayers ? "2" : "1");
            }
        }

        private bool IsTwoPlayers
        {
            get
            {
                return (Game.Services.GetService(typeof(GameFlow)) as GameFlow).IsTwoPlayers;
            }
            set
            {
                (Game.Services.GetService(typeof(GameFlow)) as GameFlow).IsTwoPlayers = value;
            }
        }

        public MainMenuScreen(Game i_Game)
            : base(i_Game)
        {
            m_Background = new InvadorBackground(i_Game, @"Sprites\BG_Space01_1024x768");
            this.Add(m_Background);
            m_MainMenu = new Menu("Sprites\\MainMenu", i_Game, 5, 85);
            this.Add(m_MainMenu);
            m_MainMenu.MenuItemEntered += new EventHandler<MenuEventArgs>(m_MainMenu_MenuItemEntered);
            m_MainMenu.MenuItemPgDown += new EventHandler<MenuEventArgs>(m_MainMenu_MenuItemPgDown);
            m_MainMenu.MenuItemPgUp += new EventHandler<MenuEventArgs>(m_MainMenu_MenuItemPgDown);

            m_MainMenu[(int)MainMenu.PlayersNum].Value = NumOfPlayers;
        }

        private void m_MainMenu_MenuItemPgDown(object sender, MenuEventArgs e)
        {
            switch (e.MenuItemIndex)
            {
                case (int)MainMenu.PlayersNum:
                    this.IsTwoPlayers = !this.IsTwoPlayers;
                    m_MainMenu[(int)MainMenu.PlayersNum].Value = NumOfPlayers;
                    break;
                default:
                    break;
            }
        }

        private void m_MainMenu_MenuItemEntered(object sender, MenuEventArgs e)
        {
            switch (e.MenuItemIndex)
            {
                case (int)MainMenu.ScreenOpt:
                    ScreenOptionsScreen OptScr = new ScreenOptionsScreen(this.Game);
                    this.ScreensManager.Push(OptScr);
                    this.ScreensManager.SetCurrentScreen(OptScr);
                    break;
                case (int)MainMenu.SoundOpt:
                    SoundOptionsScreen OptSnd = new SoundOptionsScreen(this.Game);
                    this.ScreensManager.Push(OptSnd);
                    this.ScreensManager.SetCurrentScreen(OptSnd);
                    break;
                case (int)MainMenu.Play:
                    (Game.Services.GetService(typeof(GameFlow)) as GameFlow).RestartGameScreensForGame();
                    this.ExitScreen();
                    break;
                case (int)MainMenu.Quit:
                    this.Game.Exit();
                    break;
                default:
                    break;
            }
        }

        public override void Update(GameTime gameTime)
        {
            if (this.InputManager.KeyPressed(Keys.Escape))
            {
                this.ExitScreen();
            }
            else if (this.InputManager.KeyPressed(Keys.M))
            {
                (Game.Services.GetService(typeof(ISoundManeger)) as ISoundManeger).ToggleMute();
            }

            base.Update(gameTime);
        }
    }
}
